package main

import (
	"fmt"
	"zinx/ziface"
	"zinx/znet"
)

type PingRouter struct {
	znet.BaseRouter
}

func (pr *PingRouter) Handle(request ziface.IRequest) {
	fmt.Println("Call PingRouter Handle...")
	//先读取客户端数据，再回写ping
	fmt.Println("recv from client: msgId = ", request.GetMsgId(),
		", data = ", string(request.GetData()))
	err := request.GetConnection().SendMsg(200, []byte("ping...ping...ping"))
	if err != nil {
		fmt.Println("call back ping...ping...ping...err")
	}
}

type PongRouter struct {
	znet.BaseRouter
}

func (pr *PongRouter) Handle(request ziface.IRequest) {
	fmt.Println("Call PongRouter Handle...")
	//先读取客户端数据，再回写ping
	fmt.Println("recv from client: msgId = ", request.GetMsgId(),
		", data = ", string(request.GetData()))
	err := request.GetConnection().SendMsg(201, []byte("pong...pong...pong"))
	if err != nil {
		fmt.Println("call back pong...pong...pong...err")
	}
}

func main() {
	//1.创建一个server句柄
	s := znet.NewServer("[zinx V0.8]")
	//2.给当前zinx框架添加一个自定义的router
	s.AddRouter(0, &PingRouter{})
	s.AddRouter(1, &PongRouter{})
	//3.启动server
	s.Start()
}
